package com.ikangkang.wechat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

    WebView webview;

    private Handler mHandler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        // to support html5 localstorage
        webview.getSettings().setDomStorageEnabled(true);
        webview.setWebViewClient(new MyWebViewClient());
        webview.setWebChromeClient(new MyWebChromeClient());

        webview.loadUrl("file:///android_asset/wechat_messages/wk_portal.html");

        // Button button = (Button) findViewById(R.id.button);
        // button.setOnClickListener(new Button.OnClickListener() {
        // public void onClick(View v) {
        // Log.d("MainActivity", "button OnClick");
        // webview.loadUrl("javascript:updateHtml()");
        // }
        // });

        // Button button1 = (Button) findViewById(R.id.button1);
        // button1.setOnClickListener(new Button.OnClickListener() {
        // public void onClick(View v) {
        // Log.d("MainActivity", "button1 OnClick");
        // webview.loadUrl("http://128.128.191.62/cn/mobile/geturl.html");
        // // return
        // }
        // });

        // webview.addJavascriptInterface(this, "loadArguments");
    }

    class MyWebChromeClient extends WebChromeClient {

        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                JsResult result) {
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
                    .show();
            return true;
        }

    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    webview.loadUrl("javascript:loadArguments()");
                }
            });
        }
    }

    public void startFunction() {
        AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
        ab.setTitle("title");
        ab.setMessage("ͨjs invoke java");
        ab.setPositiveButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ab.create().show();
    }
}